package com.vip8.recorder.service.utils;

import com.vip8.recorder.service.model.LoginInfo;

public class UserThreadLocal {

    private UserThreadLocal() {
    }

    private static final ThreadLocal<LoginInfo> LOGIN_THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 清除信息
     */
    public static void clear() {
        LOGIN_THREAD_LOCAL.remove();
    }

    /**
     * 保存用户信息
     *
     * @param loginInfo
     */
    public static void set(LoginInfo loginInfo) {
        LOGIN_THREAD_LOCAL.set(loginInfo);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static LoginInfo getCurrentUser() {
        return LOGIN_THREAD_LOCAL.get();
    }

    /**
     * 获取当前用户id
     * @return
     */
    public static Long getCurrentUserId() {
        LoginInfo loginInfo = LOGIN_THREAD_LOCAL.get();
        if (loginInfo != null) {
            return loginInfo.getId();
        } else {
            return null;
        }
    }

}
